<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitealbum
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Controller.php 2011-08-026 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitealbum_Widget_ListPopularAlbumsController extends Engine_Content_Widget_Abstract {

  public function indexAction() {
    // Should we consider views or comments popular?
    $popularType = $this->_getParam('popularType', 'comment');
    if (!in_array($popularType, array('comment', 'view', 'like'))) {
      $popularType = 'comment';
    }
    $this->view->popularType = $popularType;
    $this->view->popularCol = $popularCol = $popularType . '_count';
    $popularAlbum = Engine_Api::_()->getApi('settings', 'core')->getSetting('sitealbum.badgeviewer', null);
    // Get paginator
    $table = Engine_Api::_()->getItemTable('album');
    $select = $table->select()
                    ->where('search = ?', true)
                    ->order($popularCol . ' DESC');
    if (!Engine_Api::_()->sitealbum()->canShowSpecialAlbum())
      $select->where('type IS NULL');
    $this->view->paginator = $paginator = Zend_Paginator::factory($select);

    // Set item count per page and current page number
    $paginator->setItemCountPerPage($this->_getParam('itemCountPerPage', 4));
    $paginator->setCurrentPageNumber($this->_getParam('page', 1));

    // Do not render if nothing to show
    if (($paginator->getTotalItemCount() <= 0) || empty($popularAlbum)) {
      return $this->setNoRender();
    }
  }

}